package com.netty.helloworld;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

public class Server {

	public static void main(String[] args) throws Exception {
		//创建两个线程组
		//一个用于处理服务器端接收客户端连接
		//一个是进行网络通信的(网络读写)
		EventLoopGroup pGroup = new NioEventLoopGroup();
		EventLoopGroup cGroup = new NioEventLoopGroup();
		
		//创建辅助工具类，用于服务器通道的一系列配置
		ServerBootstrap bootstrap = new ServerBootstrap();
		bootstrap.group(pGroup,cGroup)//绑定两个线程组
		.channel(NioServerSocketChannel.class)	//指定NIO的模式
		.option(ChannelOption.SO_BACKLOG, 1024)	//设置tcp缓冲区
		.option(ChannelOption.SO_SNDBUF, 32*1024)//设置发送缓冲大小
		.option(ChannelOption.SO_RCVBUF, 32*1024)//设置接收缓冲区大小
		.option(ChannelOption.SO_KEEPALIVE, true)//保持连接
		.childHandler(new ChannelInitializer<SocketChannel>() {
			@Override
			protected void initChannel(SocketChannel sc) throws Exception {
				//配置具体数据接收方法的处理
				sc.pipeline().addLast(new ServerHandler());
			}
		});
		//进行绑定
		ChannelFuture cf = bootstrap.bind(8765).sync();
		//等待关闭
		cf.channel().closeFuture().sync();
		pGroup.shutdownGracefully();
		cGroup.shutdownGracefully();
		
	}
}
